package com.per.note.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.per.note.R;
import com.per.note.db.UserDao;
import com.per.note.ui.main.MainActivity;

public class RegisterActivity extends AppCompatActivity {

//    PersonDBdao persondbdao;
    UserDao userDao = new UserDao(this);
    private EditText mEditTextName;// 账号
    private EditText mEditTextPwd1;// 密码
    private EditText mEditTextPwd2;// 密码

    private String name;
    private String pwd1;
    private String pwd2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        setContentView(R.layout.activity_register);

        mEditTextName = (EditText) this.findViewById(R.id.et_register_username);
        mEditTextPwd1 = (EditText) this.findViewById(R.id.et_register_new_pwd);
        mEditTextPwd2 = (EditText) this.findViewById(R.id.et_register_confirm_pwd);
    }

    public void onClick(View v) {
        switch(v.getId()){
            case R.id.bt_register_ok:
                name=mEditTextName.getText().toString();
                pwd1=mEditTextPwd1.getText().toString().trim();
                pwd2=mEditTextPwd2.getText().toString().trim();

                if (name.equals("")) {
                    Toast.makeText(getApplicationContext(), "账户名不能为空！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                if (pwd1.equals("")) {
                    Toast.makeText(getApplicationContext(), "密码不能为空！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                if (!pwd1.equals(pwd2)) {
                    Toast.makeText(getApplicationContext(), "确认密码不同！",
                            Toast.LENGTH_SHORT).show();
                    break;
                }
                userDao.add(name,pwd2);
                Toast.makeText(getApplicationContext(), "注 册 成 功 ！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
//                intent.putExtra("name", name);
//                intent.putExtra("password",pwd2);
                // 传值 帐户名
                this.startActivity(intent);
//                finish();
                break;
            case R.id.bt_register_cancel:
                finish();
                break;
        }

    }
}
